package com.lm.liaoming.base;


import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.lm.liaoming.dialogtiplib.util.AppUtils;
import com.lm.liaoming.tool.Config;
import com.lm.liaoming.tool.FileUtil;
import com.tumblr.remember.Remember;

import org.litepal.LitePal;

import androidx.multidex.MultiDex;

/**
 * 韦敏敏
 */
public abstract class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //项目默认值
        LitePal.initialize(this);
        FileUtil.createProjectDirection();  //创建项目文件夹
        Remember.init(getApplicationContext(), Config.getProjectName() + ".share");
        AppUtils.init(getApplicationContext());
        //项目初始化
        init();
    }

    private static BaseApplication myApplication = null;

    public static BaseApplication getApplication() {
        return myApplication;
    }

    /**
     * 判断是不是UI主进程，因为有些东西只能在UI主进程初始化
     */
    public static boolean isAppMainProcess() {
        try {
            int pid = android.os.Process.myPid();
            String process = getAppNameByPID(BaseApplication.getApplication(), pid);
            if (TextUtils.isEmpty(process)) {
                return true;
            } else
                return "com.dai.app".equalsIgnoreCase(process);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    /**
     * 根据Pid得到进程名
     */
    public static String getAppNameByPID(Context context, int pid) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == pid) {
                return processInfo.processName;
            }
        }
        return "";
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    protected abstract void init();
}
